package com.slingshot.identitylibrary.idinfo

import com.slingshot.identitylibrary.idinfo.callback.IDInfoCallback
import com.slingshot.identitylibrary.idinfo.presenter.IdInforPresenter
import io.reactivex.disposables.Disposable

internal class IdInfo {

    private lateinit var idInforPresenter: IdInforPresenter

    private var idCardFrontDisposable: Disposable? = null
    private var idCardBackDisposable: Disposable? = null
    private var faceDetectDisposable: Disposable? = null

    private var extractIdCardFaceCallback: IDInfoCallback? = null
    private var extractIdCardNationalEmblemCallback: IDInfoCallback? = null
    private var faceDetectCallback: IDInfoCallback? = null

    init {
        init()
    }

    private fun init() {
        idInforPresenter = IdInforPresenter()
    }

    fun extractIdCardFace(appId: String, appSecret: String, imageBase64: String, idCardFrontCallback: IDInfoCallback) {
        this.extractIdCardFaceCallback = idCardFrontCallback
        this.extractIdCardFaceCallback?.extractStart()
        idCardFrontDisposable = idInforPresenter.extractIdCardFace(appId, appSecret, imageBase64)
            .subscribe(
                { result -> extractIdCardFaceSuccess(result) },
                { e -> extractIdCardFaceFail(e) })
    }

    private fun extractIdCardFaceSuccess(result: String) {
        extractIdCardFaceCallback?.extractSuccess(result)
    }

    private fun extractIdCardFaceFail(e: Throwable) {
        extractIdCardFaceCallback?.extractFail(e)
    }

    fun extractIdCardNationalEmblem(appId: String, appSecret: String, imageBase64: String, idCardBackCallback: IDInfoCallback) {
        this.extractIdCardNationalEmblemCallback = idCardBackCallback
        this.extractIdCardNationalEmblemCallback?.extractStart()
        idCardBackDisposable = idInforPresenter.extractIdCardNationalEmblem(appId, appSecret, imageBase64)
            .subscribe(
                { result -> extractIdCardNationalEmblemSuccess(result) },
                { e -> extractIdCardNationalEmblemFail(e) })
    }

    private fun extractIdCardNationalEmblemSuccess(result: String) {
        extractIdCardNationalEmblemCallback?.extractSuccess(result)
    }

    private fun extractIdCardNationalEmblemFail(e: Throwable) {
        extractIdCardNationalEmblemCallback?.extractFail(e)
    }

    fun extractFaceDetect(appId: String, appSecret: String, imageBase64: String, faceDetectCallback: IDInfoCallback) {
        this.faceDetectCallback = faceDetectCallback
        this.faceDetectCallback?.extractStart()
        faceDetectDisposable = idInforPresenter.faceDetect(appId, appSecret, imageBase64)
            .subscribe(
                { result -> extractFaceDetectSuccess(result) },
                { e -> extractFaceDetectFail(e) })
    }
    fun extractFaceDetect(appId: String, appSecret: String, imageBase64: String,imageBase64_ID: String, faceDetectCallback: IDInfoCallback) {
        this.faceDetectCallback = faceDetectCallback
        this.faceDetectCallback?.extractStart()
        faceDetectDisposable = idInforPresenter.faceDetect(appId, appSecret, imageBase64,imageBase64_ID)
            .subscribe(
                { result -> extractFaceDetectSuccess(result) },
                { e -> extractFaceDetectFail(e) })
    }

    private fun extractFaceDetectSuccess(result: String) {
        faceDetectCallback?.extractSuccess(result)
    }

    private fun extractFaceDetectFail(e: Throwable) {
        faceDetectCallback?.extractFail(e)
    }

    fun release() {
        idCardFrontDisposable?.dispose()
        idCardBackDisposable?.dispose()
        faceDetectDisposable?.dispose()

        extractIdCardFaceCallback = null
        extractIdCardNationalEmblemCallback = null
        faceDetectCallback = null
    }
}